using FytSoa.Domain.Sys;
using FytSoa.Sugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using MapsterMapper;
using FytSoa.Common.Utils;
using FytSoa.Domain.Core.Param;
using FytSoa.Domain.Core.Result;
using Masuit.Tools.Linq;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Sys;

/// <summary>
/// 留言消息表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v1")]
public class SysMessageService : SugarRepository<SysMessage>,ISysMessageService 
{
    private readonly IMapper _mapper;
    public SysMessageService(IMapper mapper)
    {
        _mapper = mapper;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<ApiResult<PageResult<SysMessageDto>>> GetPagesAsync(PageParam param)
    {
        var where = Expressionable.Create<SysMessage>()
            .AndIF(!string.IsNullOrEmpty(param.Key), m => m.Tags.Contains(param.Key) || m.Title.Contains(param.Key) || m.Summary.Contains(param.Key))
            .AndIF(param.Status=="1", m => m.IsRead)
            .AndIF(param.Status == "2", m => !m.IsRead)
            .AndIF(param.Status == "3", m => m.IsDelete);
        var (list,totalItems,totalPage) = await base.GetPageResultAsync(where.ToExpression(), param.Page, param.Limit, m => m.Id);
        var result = new PageResult<SysMessageDto>()
        {
            Items = _mapper.Map<List<SysMessageDto>>(list),
            TotalItems = totalItems,
            TotalPages = totalPage
        };
        foreach (var item in result.Items)
        {
            item.TypesName = item.Types.ToString();
        }
        return JResult<PageResult<SysMessageDto>>.Success(result);
    }
    
    /// <summary>
    /// 查询消息的汇总
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ApiResult<SysMessageTotalDto>> GetTotalAsync()
    {
        var allCount = await base.CountAsync (m => true);
        var unReadCount = await base.CountAsync (m => !m.IsRead);
        var recycleCount = await base.CountAsync (m => m.IsDelete);
        var model = new SysMessageTotalDto () {
            AllCount = allCount,
            UnReadCount = unReadCount,
            RecycleCount = recycleCount
        };
        return JResult<SysMessageTotalDto>.Success (model);
    }


    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ApiResult<SysMessageDto>> GetAsync(long id)
    {
        return JResult<SysMessageDto>.Success(_mapper.Map<SysMessageDto>(await base.GetByIdAsync(id)));
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<ApiResult<bool>> AddAsync(SysMessageDto model)
    {
        model.Id = Unique.Id();
        return JResult<bool>.Success(await base.InsertAsync(_mapper.Map<SysMessage>(model)));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<ApiResult<bool>> ModifyAsync(SysMessageDto model)
    {
        return JResult<bool>.Success(await base.UpdateAsync(_mapper.Map<SysMessage>(model)));
    }
    
    /// <summary>
    /// 设置已读
    /// </summary>
    /// <returns></returns>
    [HttpPut]
    public async Task<ApiResult<bool>> ReadAsync ([FromBody]List<long> ids)
    {
        return ids.Count>0 
            ? JResult<bool>.Success (await base.UpdateAsync (m => new SysMessage(){ IsRead = true}, m => ids.Contains (m.Id))) 
            : JResult<bool>.Success (await base.UpdateAsync (m => new SysMessage(){ IsRead = true}, m => true));
    }
    
    /// <summary>
    /// 删除到回收站
    /// </summary>
    /// <returns></returns>
    [HttpDelete]
    public async Task<ApiResult<bool>> RecycleAsync (string ids) {
        return JResult<bool>.Success (await base.UpdateAsync (m =>new SysMessage(){ IsDelete = true}, m => ids.StrToListLong().Contains (m.Id)));
    }

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<ApiResult<bool>> DeleteAsync(string ids)
    {
        return JResult<bool>.Success(await base.DeleteAsync(m=>ids.StrToListLong().Contains(m.Id)));
    }
}
